package com.sk.weichat.ui.nearby;

import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.base.EditBaseActivity;
import com.sk.weichat.util.MyFragmentManager;

import de.greenrobot.event.EventBus;

/**
 * 添加好友
 */
public class UserListGatherActivity extends EditBaseActivity {

    private MyFragmentManager mMyFragmentManager;
    private SearchUserFragment mSearchFriendFragment;
    public EditText etSearch;
    private TextView tvCancel, tvSearchText;
    public LinearLayout ll_layout_search_empty;
//    public TextView tvEmptyUser;
    public LinearLayout linearTip;
    private ImageView ivClear;

    private String search_user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list_gather);
        getSupportActionBar().hide();
        mMyFragmentManager = new MyFragmentManager(this, R.id.fl_fragments);
        mSearchFriendFragment = new SearchUserFragment();
        mMyFragmentManager.add(mSearchFriendFragment);
        mMyFragmentManager.show(0);
        initView();
    }

    private void initView() {
        etSearch = findViewById(R.id.et_search_user);
        tvCancel = findViewById(R.id.tv_finish_search_user);
        tvSearchText = findViewById(R.id.tv_search_text);
        ll_layout_search_empty = findViewById(R.id.ll_layout_search_empty);
//        tvEmptyUser = findViewById(R.id.tv_empty_user_tips);
        linearTip = findViewById(R.id.linear_search_tip);
        ivClear = findViewById(R.id.iv_clear_user);


        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                ll_layout_search_empty.setVisibility(View.GONE);
//                tvEmptyUser.setVisibility(View.GONE);
                mSearchFriendFragment.clearData();
                if (s == null || s.length() == 0) {
                    ivClear.setVisibility(View.INVISIBLE);
                    linearTip.setVisibility(View.GONE);
                    return;
                }
                ivClear.setVisibility(View.VISIBLE);
                linearTip.setVisibility(View.VISIBLE);
//                String tipStr = "搜索用户：<font color='#8d76fe'>" + s + "</font>";
                String tipStr = "网络查找“<font color='#8d76fe'>" + s + "</font>”相关的人";

                tvSearchText.setText(Html.fromHtml(tipStr));

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    mSearchFriendFragment.setMsg(etSearch.getText().toString(), 0, 0, 200, 0);
                    mSearchFriendFragment.requestData(true,true);
                    linearTip.setVisibility(View.GONE);
                    return true;
                }
                return false;
            }
        });
        linearTip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchFriendFragment.setMsg(etSearch.getText().toString().trim(), 0, 0, 200, 0);
                mSearchFriendFragment.requestData(true,true);
                linearTip.setVisibility(View.GONE);
            }
        });
        ivClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etSearch.setText("");
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post("cancle");
                finish();
            }
        });
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        /*延迟加载 等待控件初始化*/
        new Handler().postDelayed(new Runnable(){
            public void run() {
                search_user = getIntent().getStringExtra("search_user");
                if (!TextUtils.isEmpty(search_user)){
                    etSearch.setText(search_user);
                    etSearch.setSelection(search_user.length());
                    mSearchFriendFragment.setMsg(search_user, 0, 0, 200, 0);
                    mSearchFriendFragment.requestData(true,true);
                    linearTip.setVisibility(View.GONE);
                }
            }
        }, 50);
    }
}
